home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / text / hyper / ag2html.lha / AG2HTML.pl
Encoding:
Perl Script  |  1994-06-18  |  8.6 KB  |  220 lines

  1. #!/usr/local/bin/perl
  2. # <title> AG2HTML.pl </title>
  3. # <h1> AG2HTML.pl Amiga Guide to HTML Converter</h1>
  4. # <listing>
  5. # <b>This code is (c) 1993,1994 to Michael Witbrock</b>
  6. # <b>This code is © 1993,1994 to Michael Witbrock</b>
  7. # <b>You may use it and modify and redistribute it freely,</b>
  8. # <b>but you may not sell it in any way (including in disk collections)</b>
  9. # <b>without first recieving my permission.</b>
  10. # <b>Fred Fish, and the makers of the Aminet CD are granted that permission.
  11. # <b>If you significantly improve it, please let me know so that I can</b>
  12. # <b>use the new version.</b>
  13. # <b>You may contact me as witbrock@cs.cmu.edu</b>
  14.  
  15. $VERSION = "2.94618";
  16.  
  17. #http://www.cs.cmu.edu:8001/Web/People/mjw/Computer/Amiga/Perl/AG2HTML.pl
  18. #is a URL to the latest version of this program.
  19.  
  20. # P.S. I know that this is redundant. It's my second perl script, and I don't
  21. #     yet know how to do subroutines. When I do, it will be both neater and shorter.
  22. # P.P.S the reason it is preformatted is because many AG documents have button
  23. #     layouts which depend on this. Perhaps later I will make it smarter, so that
  24. #     it can recognise obvious text paragraphs and do the right thing.
  25. # DONE: P.P.P.S handling some of the style flags, and the next, menu, prev, help, etc buttons
  26. #      is next, along with removing what can't be done (background colours etc).
  27.  
  28.  
  29. #Changes: October 16 1993 MJW
  30. #         Allow email addresses.
  31. #         Translate & to & > to > < to < 
  32. #Changes: November the 9th 1993 MJW
  33. #         Allow some strange links that have strings after the link name
  34. #         found in Viewport
  35. #Changes: March 12 1994 MJW
  36. #         Allow Link whereever I allow link -after Steve Gowdy's suggestion-
  37. #Changes: April 22 1994 MJW
  38. #         Major rewrite?
  39. #         for Daniel Barrett, @{"mybutton" system "more myfile"} -> <A HREF="myfile">mybutton</A>
  40. #          THE next version will handle pictures in AmigaReport files automatically, but that may be a few days off.
  41. #          Picture conversions done
  42. #          Automatic inlining of pictures done for ones like the following 
  43.  
  44. #UNHANDLED?: @{"Lynx" system "display.s 10,100,AR215_pic1.iff,Lynx"} is a user-friendly hypertext interface on UNIX and VMS 
  45. #UNHANDLED?: The @{"Commodore Amiga Information Resource" system "display.s 10,100,AR215_pic2.iff,CAIR"} is a collection 
  46. #UNHANDLED?: - @{"Amiga Report" system "display.s 10,100,AR215_pic3.iff,AR on the WWW"}
  47.  
  48. #Changes May 28th 1994
  49. # Amiga report changed to display like this:
  50. # @{" AR Logo " system "display.s 650 100 AR217_pic2.iff Amiga_Report"}
  51. # Change it to handle that too.
  52. # June 9th ---- Changed pointer to itself
  53. # June 18th ---- version numbers, so I can tell people when it changes
  54.  
  55. sub handlenode{
  56.     while (<guidef>){
  57.         chop;
  58.         if (m/@[eE][nN][dD][Nn][oO][dD][Ee]/){
  59.         # print "Found @endnode\n";            # found @endnode
  60.         print pagef "</pre>\n"; # 
  61.         if ($buttonline ne "<HR>") { print pagef "$buttonline"; }
  62.         print pagef "<HR>HTML Conversion by <a href=\"http://www.cs.cmu.edu:8001/Web/People/mjw/Computer/Amiga/Perl/AG2HTML.pl\"><i>AG2HTML.pl</i></a> V${VERSION} & <a href=\"http://www.cs.cmu.edu:8001/Web/People/mjw/mjwhome.html\"><i>witbrock@cs.cmu.edu</i></a>\n";
  63.         close (pagef);    # 
  64.         last;        # 
  65.         } else {
  66.         # Remember TOC link, and delete the line if found
  67.         if (m/@[tT][oO][cC]\s*"?([^\"\s\}]*)"?/){
  68.         $buttonline = $buttonline."<a href=\"$1\.HTML\">[Contents]<\/a> ";
  69.         next;        # 
  70.         }            # 
  71.         # Remember prev link, and delete the line if found
  72.         if (m/@[pP][rR][eE][vV]\s*"?([^\"\s\}]*)"?/){
  73.         $buttonline = $buttonline."<a href=\"$1\.HTML\">[Browse <-]<\/a> ";
  74.         next;
  75.         }
  76.         # Remember next link, and delete the line if found
  77.         if (m/@[nN][eE][xX][tT]\s*"?([^\"\s\}]*)"?/){
  78.         $buttonline = $buttonline."<a href=\"$1\.HTML\">[Browse ->]<\/a> ";
  79.         next;
  80.         }
  81.         # Remember help link, and delete the line if found
  82.         if (m/@[hH][eE][lL][pP]\s*"?([^\"\s\}]*)"?/){
  83.         $buttonline = $buttonline."<a href=\"$1\.HTML\">[Help]<\/a> ";
  84.         next;
  85.         }
  86.         # otherwise look for more calls, links, or plain text
  87.         study;
  88.         s/&/&/g;
  89.         s/>/>/g;
  90.         s/</</g;
  91.         # Handle pictures (as inline maybe change eventually)
  92.         s/@\{\s*\"([^\"]*)\"\s+[sS][yY][Ss][tT][eE][mM]\s+\"displa\S* [0-9,\s]*(\S*)\.iff[,\s]*([^\"\}]*)\"*\s*\}/<p>\<img src=\"$2\.gif\"\><br>$3<p>$1/g;
  93.  
  94.         # for Daniel Barrett, @{"mybutton" system "more myfile"} -> <A HREF="myfile">mybutton</A>
  95.         s/@\{\s*\"([^\"]*)\"\s+[sS][yY][Ss][tT][eE][mM]\s+\"more *([^\"\s\}]*)\"*\s*\}/\<a href=\"$2\"\>$1<\/a>/g;
  96.         #link
  97.         s/@\{\s*\"([^\"]*)\"\s+[Ll][iI][nN][kK]\s+\"*([^\"\s\}]*)\"*\s*\}/\<a href=\"$2\.HTML\"\>$1<\/a>/g;
  98.         # found in viewport -- link with string after don't know what means
  99.         s/@\{\s*\"([^\"]*)\"\s+[Ll][iI][nN][kK]\s+\"*([^\"\s\}]*)\"*\s*\"[^\"]*\"\s*\}/\<a href=\"$2\.HTML\"\>$1<\/a>/g;   
  100.         # found in kingcon -- link with number after don't know what means
  101.         s/@\{\s*\"([^\"]*)\"\s+[Ll][iI][nN][kK]\s+\"*([^\"\s\}]*)\"*\s*[0123456789]*\s*\}/\<a href=\"$2\.HTML\"\>$1<\/a>/g;   
  102.         s/@\{[bB]\}/<B>/g;  s/@\{[uU][bB]\}/<\/B>/g; # bold
  103.         s/@\{[iI]\}/<I>/g;  s/@\{[uU][iI]\}/<\/I>/g; #italic
  104.         if (m/@\{/) {        # Recognise and hide unhandled cases 
  105.         print "UNHANDLED?: $_\n";
  106.         s/(@\{[^\}]*\})/<!- Unhandled AmigaGuide(TM) sequence "$1">/g;
  107.         }                   
  108.         print pagef  "$_\n";
  109.         }
  110.     }
  111. }
  112.  
  113. die "Usage: %0 <AmigaGuideFile> $!\n N.B. This program puts AG node HTML files in a dir.\n"
  114.     unless ( $#ARGV == 0 );
  115. $agname = $ARGV[0]; $_ = $agname;
  116. if (/(.*)\.guide/){
  117.   $root = $1;
  118. } else {
  119.   die "Error: $agname doesn't seem to be an AmigaGuide(TM) file.\n";
  120. }
  121.  
  122. $dirname = $root."_Sections";
  123. mkdir($dirname,oct(777)) unless (-e $dirname);
  124. die "Couldn't make $dirname \n" unless -e $dirname;
  125. open (guidef, $agname) || die "Can't open $agname: $!\n";
  126.      
  127. $databasefound=0;
  128. # Move suitably labelled pictures to go with the file 
  129. system ("mv ${root}_\*\.iff $dirname");
  130. # Convert them to something mosaic can handle
  131. opendir(sect,$dirname) || die "Can't read dir $dirname\n";
  132. while ($_ = readdir(sect)){
  133.     next unless m#(.*)\.iff#;
  134.     print "Converting picture $1 to gif \n" unless -e  "${dirname}/$1.gif";
  135.     print "Founded gif version of picture $1 \n" if -e  "${dirname}/$1.gif";
  136.     system("ilbmtoppm < ${dirname}/$1.iff | ppmtogif > ${dirname}/$1.gif")
  137.     unless -e  "${dirname}/$1.gif";
  138.  
  139. }
  140. closedir(sect);
  141.     
  142. while (<guidef>) {
  143.     chop;
  144.     if ((m/@[dD][Aa][Tt][Aa][bB][aA][sS][eE]\s*"(.*)"/)
  145.     || (m/@[dD][Aa][Tt][Aa][bB][aA][sS][eE]\s*(\S*)/)){
  146.     if ($databasefound != 0) {
  147.         print "IGNORED: database label $1 found after first one $database\n";
  148.         next;
  149.     }
  150.     $databasefound = 1;
  151.     $database=$1;
  152.     print "Database: $database\n";
  153.     while (<guidef>){
  154.         chop;
  155.         $2="Untitled";
  156.         $buttonline = "<HR>";
  157.         if ( (m/@[Nn][oO][dD][Ee]\s*"(\S*)"\s*"(.*)"/)
  158.         || (m/@[Nn][oO][dD][Ee]\s*(\S*)\s*"(.*)"/)
  159.         || (m/@[Nn][oO][dD][Ee]\s*(\S*)/)
  160.         ){  
  161.         # found @Node LABEL "title"
  162.         # or just @node Label or even @node "label" "title"
  163.         $nodename = $1;
  164.         $nodetitle = $2;
  165.         $htmlname = $dirname."/".$1.".HTML";
  166.         if (-e $htmlname) {
  167.             unlink($htmlname);
  168.         }
  169.         open (pagef,'>'.$htmlname)
  170.             || die "Can't open $htmlname to write $!\n";
  171.         print pagef "<HTML>\n<TITLE>$nodetitle</TITLE>\n";
  172.         print pagef "<H1>$nodetitle</H1>\n<pre>\n";
  173.         last;
  174.         } else {
  175.         if (m/\S*/) {print "# SKIPPED while looking for @node: $_ \n"; }
  176.         next;
  177.         }
  178.     }   # Found first @node line
  179.     print "NOTE: main node is $htmlname\n";
  180.     # Now look for end of first node
  181.     &handlenode;
  182.     # found end of first node, or of the file     
  183.     } else { # No database label on this line 
  184.     if ($databasefound == 0){  # stuff before first @database is ignored
  185.         print "#SKIPPED while looking for database: $_\n";
  186.         next;
  187.     }
  188.     # otherwise, it may be a perfectly good line
  189.     # check whether it is the start of a new node
  190.     $2="Untitled";
  191.     $buttonline = "<HR>";
  192.     if ( (m/@[Nn][oO][dD][Ee]\s*"(\S*)"\s*"(.*)"/)
  193.         || (m/@[Nn][oO][dD][Ee]\s*(\S*)\s*"(.*)"/)
  194.         || (m/@[Nn][oO][dD][Ee]\s*(\S*)/)
  195.         ){  # found @Node LABEL "title", @node Label, or even @node "label" "title"
  196.         $nodename = $1;
  197.         $nodetitle = $2;
  198.         $htmlname = $dirname."/".$1.".HTML";
  199.         unlink($htmlname) if -e $htmlname;
  200.         open (pagef,'>'.$htmlname)
  201.         || die "Can't open $htmlname to write $!\n";
  202.         print pagef "<HTML>\n<TITLE>$nodetitle</TITLE>\n";
  203.         print pagef "<H1>$nodetitle</H1>\n<pre>\n";
  204.         
  205.         # print "Found Node: $nodename $nodetitle as $htmlname \n";
  206.         # since we found the beginning of the node, copy to the end
  207.         &handlenode;
  208.     } else {   # Haven't yet found a @node
  209.         if (m/\S*/) {print "# SKIPPED while looking for @node: $_ \n"; }
  210.         next;
  211.     }
  212.     }
  213. }
  214.  
  215. close guidef;
  216.  
  217. # </listing>
  218.